package com.cmcc.qx.common.dao.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.cmcc.qx.common.dao.BaseDao;

/**
 * Dao层基类实现
 * 
 * @author zhangxiaobo
 */
@Repository("BaseDaoImpl")
public class BaseDaoImpl implements BaseDao {

	@Autowired
	protected SessionFactory sessionFactory;

	public Object getObj(Class<?> clazz, Serializable id) {
		try {
			return sessionFactory.getCurrentSession().get(clazz, id);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public Serializable save(Object obj) {
		return sessionFactory.getCurrentSession().save(obj);
	}

	public void update(Object obj) {
		sessionFactory.getCurrentSession().update(obj);
	}

	public void delete(Object obj) {
		sessionFactory.getCurrentSession().delete(obj);
	}

	@SuppressWarnings("unchecked")
	public List<Object> queryHql(String hql, Object... params) {
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		for (int i = 0; i < params.length; i++) {
			query.setParameter(i, params[i]);
		}
		List<Object> list = new ArrayList<Object>();
		try {
			list = (List<Object>) query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<Object> queryHql(int start ,int limit, String hql, Object... params) {
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setFirstResult(start);
		query.setMaxResults(start+limit);
		for (int i = 0; i < params.length; i++) {
			query.setParameter(i, params[i]);
		}
		List<Object> list = new ArrayList<Object>();
		try {
			list = (List<Object>) query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Object> querySql(int start ,int limit, String sql,Class clazz, Object... params) {
		Query query = sessionFactory.getCurrentSession().createSQLQuery(sql).addEntity(clazz);
		query.setFirstResult(start);
		query.setMaxResults(start+limit);
		for (int i = 0; i < params.length; i++) {
			query.setParameter(i, params[i]);
		}
		List<Object> list = new ArrayList<Object>();
		try {
			list = (List<Object>) query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public List<Object> querySql(String sql, Object... params) {
		Query query = sessionFactory.getCurrentSession().createSQLQuery(sql);
		for (int i = 0; i < params.length; i++) {
			query.setParameter(i, params[i]);
		}
		List<Object> list = new ArrayList<Object>();
		try {
			list = (List<Object>) query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public List<Object> querySqlClass(String sql, Class clazz,Object... params) {
		Query query = sessionFactory.getCurrentSession().createSQLQuery(sql).addEntity(clazz);
		for (int i = 0; i < params.length; i++) {
			query.setParameter(i, params[i]);
		}
		List<Object> list = new ArrayList<Object>();
		try {
			list = (List<Object>) query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public void sqlExecute(String sql) {
		sessionFactory.getCurrentSession().createSQLQuery(sql).executeUpdate();
	}
}
